﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Sbook.AdminIstrator
{
    public partial class DoanhThuPrint : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Data.Mysession.isLogin)
                {
                    if (Request.QueryString["tungay"] != null && Request.QueryString["denngay"] != null)
                    {
                        BindListDelivery(Request.QueryString["tungay"], Request.QueryString["denngay"]);
                    }
                    
                }
                else
                    Response.Redirect("/AdminIstrator/Login.aspx");
            }
            
        }

        private void BindListDelivery(string fromDate,string toDate)
        {
            List<Data.DeliveryInfo> listDelivery = new List<Data.DeliveryInfo>();
            if (fromDate.Equals("") && toDate.Equals(""))
            {
                listDelivery = Business.DeliveryService.Delivery_GetByTop("", "DeliveryStatus=1", "");
                lblDate.Text = "TỔNG DOANH THU";

            }
            else if (fromDate.Equals("") && !toDate.Equals(""))
            {
                listDelivery = Business.DeliveryService.Delivery_GetByTop("", "DeliveryStatus=1 and DateDiff(day,DeliveryDate,'" + toDate + "')>=0", "");
                lblDate.Text = "DOANH THU ĐẾN NGÀY: " + toDate;
            }
            else if (!fromDate.Equals("") && toDate.Equals(""))
            {
                listDelivery = Business.DeliveryService.Delivery_GetByTop("", "DeliveryStatus=1 and DateDiff(day,'" + fromDate + "',DeliveryDate)>=0", "");
                lblDate.Text = "DOANH THU TỪ NGÀY: " + fromDate;
            }
            else
            {
                listDelivery = Business.DeliveryService.Delivery_GetByTop("", "DeliveryStatus=1 and DateDiff(day,'" + fromDate + "',DeliveryDate)>=0 and DateDiff(day,DeliveryDate,'" + toDate + "')>=0", "");
                lblDate.Text = "DOANH THU TỪ NGÀY " + fromDate + " ĐẾN NGÀY " + toDate;
            }
            rpDelivery.DataSource = listDelivery;
            rpDelivery.DataBind();
            int sum = 0;
            for (int i = 0; i < rpDelivery.Items.Count; i++)
            {
                
                Label lblPrice = (Label)rpDelivery.Items[i].FindControl("lblPrice");
                //lblPrice.Text = lblPrice.Text.Replace(".0000", "");
                if (lblPrice.Text.Replace(".0000", "") != "")
                {
                    sum += Convert.ToInt32(lblPrice.Text.Replace(".0000", ""));
                }
                lblPrice.Text = Format_Price(lblPrice.Text.Replace(".0000", ""));

                Label lblDate1 = (Label)rpDelivery.Items[i].FindControl("lblDate1");
                Label lblDate2 = (Label)rpDelivery.Items[i].FindControl("lblDate2");
                if (lblDate1.Text != "")
                {
                    lblDate1.Text = (Convert.ToDateTime(lblDate1.Text)).ToString("dd/MM/yyyy HH:mm");
                   
                }
                if (lblDate2.Text != "")
                {
                    lblDate2.Text = (Convert.ToDateTime(lblDate2.Text)).ToString("dd/MM/yyyy HH:mm");
                }
            }
            Label lblSumPrice = (Label)rpDelivery.Controls[rpDelivery.Controls.Count - 1].Controls[0].FindControl("lblSumPrice");
            lblSumPrice.Text = Format_Price(sum.ToString()) + " VND";

           
        }

        protected void lbtExport_Click(object sender, EventArgs e)
        {
           
            lbtExport.Visible = false;
            Response.ContentType = "application/vnd.ms-excel";
            Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment; filename=Baocao.xls");
            StringWriter stringWriter = new StringWriter(); //System.IO namespace should be used
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            htmlTextWriter.WriteLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
            this.RenderControl(htmlTextWriter);
            Response.Write(stringWriter.ToString());
            Response.End();
        }
        protected string Format_Price(string Price)
        {
            Price = Price.Replace(".", "");
            Price = Price.Replace(",", "");
            string tmp = "";
            while (Price.Length > 3)
            {
                tmp = "." + Price.Substring(Price.Length - 3) + tmp;
                Price = Price.Substring(0, Price.Length - 3);
            }
            tmp = Price + tmp;
            return tmp;
        }
        
    }
}